package com.starinside.wp.controller.api;

import com.starinside.wp.domain.WxUser;
import com.starinside.wp.domain.record.LiveRecord;
import com.starinside.wp.foundation.constant.RequestConstant;
import com.starinside.wp.foundation.dto.StaffDTO;
import com.starinside.wp.foundation.model.ErrorCode;
import com.starinside.wp.foundation.model.StatusResponse;
import com.starinside.wp.service.LiveRecordService;
import com.starinside.wp.service.UploadService;
import com.starinside.wp.utils.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.Objects;

import static com.starinside.wp.foundation.constant.RequestConstant.TICKET;

@RestController
@RequestMapping("/wx/api/upload")
@Api(value = "/wx/api/upload", tags = "文件上传")
public class APIUploadController {
    @Autowired
    private UploadService uploadService;

    @Autowired
    private LiveRecordService liveRecordService;

    @Value("${aliyun.oss.fileServerPrefix}")
    private String IMG_SERVICE_PREFIX;

    @RequestMapping(value = "/upload_live_record_img", method = RequestMethod.POST)
    @ApiOperation("上传文件【暂未实现】")
    @ApiImplicitParams({
            @ApiImplicitParam(name="files", value = "文件数组"),
            @ApiImplicitParam(name="mediaType", value = "媒体类型"),
            @ApiImplicitParam(name="category", value = "文件分类")
    })
    public StatusResponse addLiveRecord(HttpServletRequest req, HttpServletResponse resp, MultipartFile[] files, Integer mediaType, Integer category) {

        return StatusResponse.success();
    }


    @ApiOperation(value = "上传图片，每天一个文件夹")
    @RequestMapping(value = "/file" ,method = RequestMethod.POST)
    @ResponseBody
    public StatusResponse uploadFile(HttpServletRequest req, @RequestHeader(RequestConstant.TICKET)String ticket,
                                     String bizType, MultipartFile file) {
        if (Objects.isNull(file))
            return StatusResponse.error(ErrorCode.FILE_MAST_NOT_NULL,"文件不能为空");

        WxUser user = (WxUser) req.getSession().getAttribute("loginUser");
        String wxUserId = user.getId();


        String rel = DateUtil.getToday();

        if (StringUtils.isNotEmpty(bizType)){
            if (bizType.length() < 3 || bizType.length() > 20 || bizType.contains("/")) {
                return StatusResponse.error(ErrorCode.PARAM_CAN_NOT_BLANK, "参数(或长度)错误");
            }

            rel = "p_" + bizType + "/" + rel;
        }

        String url = uploadService.uploadCommonFile(wxUserId, rel, file);

        return StatusResponse.success(url);
    }
}
